<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>       
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="${_resources}/logo.png">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">                                            
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8">  
<title>自定义文档模板</title>
<%@include file="/WEB-INF/jsp/common/inc.jsp"%>
<link rel="stylesheet" href="${_resources}bootstrap/bootstrap.min.css">
<link rel="stylesheet" href="${_resources}bootstrap/bootstrap-table.min.css">
<script type="text/javascript" src="${_resources}jquery-ui/jquery-ui-1.10.4.custom.min.js"></script>
<link rel="stylesheet" href="${_resources}jquery-ui/jquery-ui-1.10.4.custom.min.css">

<script type="text/javascript"> 
var defineTemplate;
var userId ="${userId}";
var userName ="${userName}";
var documentTemplateSorts = '${documentTemplateSorts}';
var applicableScopes = '${applicableScopes}' ;
var user_id = "${user_id}";
var menuId = "${menuId}";
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length >>> 0;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}


function alterproperty(id){
	layer.open({
		  type: 2,
		  title:'修改模板数据',
		  shade:0.2,			//遮罩层颜色
		  area: ['700px', '400px'],
		  fixed: false, //不固定
		  maxmin: true,
		  content: "${_baseUrl}/documentTemplateController/returnAlterProperty?documentId="+id+"&rand="+Math.random()
		});
}

function getTableData (status){
	defineTemplate = $("#defineTemplate").bootstrapTable({
		url : '${_baseUrl}/KmUserDocumentTemplateController/selectDocumentTemplate?rand='+Math.random(),
		pagination: true,
		pageList: [5, 10, 20, 50],
  		queryParams: queryParams, 		
  		columns: [{
       	 field: 'checked',
       	 checkbox:true,
       	 formatter : function(value, row, index) {
           	 if(userId=="sysadmin"){
           		return ;
           	 }else{
           		 if(row.editerId !=null){
           			if(row.userId!=user_id){
	                		return {
	                            disabled: true
	                        };
	                	}else{
	                		return;
	                	}
           		 }else{
           			 if(row.userId!=user_id){
 	                		return {
 	                            disabled: true
 	                        };
 	                	}else{
 	                		return;
 	                	}
           		 }
               }
           }
        }, {
            field: 'editerId',
            title: 'editerId',
            visible:false,
            width: 5
        },{
            field: 'id',
            title: 'id',
            visible:false,
            width: 10
        },{
            field: 'documentTemplateName',
            title: '文档模板名称',
            width: 100,
            formatter : function(value, row, index) {
            	var str="";
        		str +="<a onClick=\"checkFun('"+row.id+"');\"  title='"+value+"' class=\"late_on_temp\">"+value+"</a>";  
			return str;
            }
        },{
            field: 'applicationScopeName',
            title: '适用范围',
            width: 100
        },{
            field: 'documentTypeName',
            title: '模板类型',
            width: 100
        },
        {
            field: 'templateStatus',
            title: '模板状态',
            width: 100,
            formatter : function(value, row, index) {
				if ("1"== value) {
					return "启用";
				}else{
					return "禁用";
				}
			}
        },{
            field: 'editer',
            title: '上传人员',
            width: 100,
            formatter : function(value, row, index) {
            	var str=value;
            	if(value=='sysadmin'){
            		str="平台初始化";
            	}
				return str;
            }
        },
        {
            field: 'createDate',
            title: '上传时间',
            width: 100
		}],
		onLoadSuccess: function (data) {
			var company = $('#company').val();
			console.log(company + "-------------------")
			if (company == 'siyuan17suo') {
				$('#defineTemplate').bootstrapTable('hideColumn', 'applicationScopeName');
			}
		},
        
       
    });
	
	function queryParams(params){
		return {
		    pageSize: params.limit,
		    pageNo: params.pn,
		    sort : params.sort,
		    order : params.order,
		    documentTemplateName:$("#docName").val()
	   }
	}
}

function getLoginInfo(){
	 var path="${_baseUrl}/documentTemplateController/returnLoginInfo";
	 $.ajax({    
			type: "POST",    
			async: false,    
			url:path,     
			dataType:"text",    
			success: function (data) {   
			    if(data=="true"||data==true){
			    	getTableData (true);
			    }else{
			    	getTableData (false);
			    }
			}   
		}); 
	
}

function getProData(){
	var path="${_baseUrl}/documentTemplateController/getProData";
	$.ajax({
		type: "POST",    
		async: false,    
		url:path,     
		dataType:"json",    
		success: function (json) {   
			documentTemplateSorts = json.data;
        	applicableScopes = json.message ;
		}   
	})
}



function judgeCurrentUse(name){
	var  userName = "${userName}";
	if(userName==name){
		return true;
	}else{
		return false;
	}
}

$(function(){	
	getTableData();
	getMenuPath();
})


function deleteFun(){
	var objects =  $("#defineTemplate").bootstrapTable('getSelections');
	var id ="";
	for(var i =0;i<objects.length;i++){
		if(id.length>0){
			id+=","+objects[i].id;
		}else{
			id=objects[i].id;
		}
	}
	if(id.length==0){
		layer.msg("请选择文档模板");
		return;
	}
	 var path="${_baseUrl}/KmGroupDocumentTemplateController/delDocumentTemplate";
	 $.ajax({    
			type: "POST",    
			async: false,    
			url:path,   
			data:{
				"id":id      
			},    
			dataType:"json",    
			success: function (data) {  
				layer.msg(data.message,{shift:5,time:3000});
				refreshTable();		
			}   
		}); 
}
function addFun(){
	layer.open({
		  type: 2,
		  title:'新增文档模板',
		  shade:0.2,			//遮罩层颜色
		  area: ['1000px', '345px'],
		  fixed: false, //不固定
		  maxmin: true,
		  content: "${_baseUrl}/KmGroupDocumentTemplateController/documentTemplateUpload?source=define"+"&rand="+Math.random()
		});
}

function completeFun(id){
	layer.open({
		  type: 2,
		  title:'新增文档模板',
		  shade:0.2,			//遮罩层颜色
		  area: ['1000px', '345px'],
		  fixed: false, //不固定
		  maxmin: true,
		  content: "${_baseUrl}/documentTemplateController/completeDocumentTemplate?id="+id+"&rand="+Math.random() 
		});
}

function delBlankDocument(){
	var path="${_baseUrl}/documentTemplateController/delBlankDocument";
	 $.ajax({    
			type: "POST",    
			async: false,    
			url:path,   
			data:{
				"type":"define"
			},    
			dataType:"json",    
			success: function (data) {   
				if(data.code=='1'){
					refreshTable();
				}else{return;}
			}   
	}); 
}

function checkFun(id){
	window.location.href="${_baseUrl}/KmGroupDocumentTemplateController/previewDocumentTemplate?id="+id+"&rand="+Math.random();
}

function useStatus(status){
	var objects =  $("#defineTemplate").bootstrapTable('getSelections');
	var id ="";
	for(var i =0;i<objects.length;i++){
		if(id.length>0){
			id+=","+objects[i].id;
		}else{
			id=objects[i].id;
		}
	}
	if(id.length==0){
		layer.msg("请选择文档模板");
		return;
	}
	var path="${_baseUrl}/KmGroupDocumentTemplateController/useDocumentTemplate";
	 $.ajax({    
			type: "POST",    
			async: false,    
			url:path,   
			data:{
				"id":id,
				"status":status
			},    
			dataType:"json",    
			success: function (data) {   
				layer.msg(data.message);
				refreshTable();	
			}   
	}); 
} 
//判断文档模板是否已经被项目模板禁用
function judgeProjectDocument(){
	var objects =  $("#defineTemplate").bootstrapTable('getSelections');
	var id ="";
	for(var i =0;i<objects.length;i++){
		if(id.length>0){
			id+=","+objects[i].id;
		}else{
			id=objects[i].id;
		}
	}
	if(id.length==0){
		layer.msg("请选择文档模板");
		return; 
	}
	var path="${_baseUrl}/KmGroupDocumentTemplateController/judgeDocumentUseStatus";
	$.ajax({    
		type: "POST",    
		async: false,    
		url:path,   
		data:{
			"id":id,
		},    
		dataType:"json",    
		success: function (json) { 
			if(json.code=='1'){
				var jsonObj=eval("("+json.message+")");
				var options="";
				if(jsonObj.length>0){
					$.each(jsonObj, function (i, item) {
						if(options.length>0){
							options+=",《"+item.documentTemplateName+"》";
						}else{
							options = "《"+item.documentTemplateName+"》";
						}
						
				 	});
				}
				options+="已在项目中使用，不能禁用 ";
				layer.msg(options,{shift:5,time:2000},function(){
					return;
				});
				
			}
			if(json.code=='2'){
				var jsonObj=eval("("+json.message+")");
				var options="";
				if(jsonObj.length>0){
					$.each(jsonObj, function (i, item) {
						if(options.length>0){
							options+=",《"+item.documentTemplateName+"》";
						}else{
							options = "《"+item.documentTemplateName+"》";
						}
						
				 	});
				}
				options+="已在项目中使用，不能禁用 ";
				layer.msg(options,{shift:5,time:2000},function(){
					return;
				});
				
			}else{
				useStatus("0");
			}
		}   
	}); 
	
}



//文档分类管理
function defineTemplateSort(){
	layer.open({
		  type: 2,
		  title:'文档模板类型定义',
		  shade:0.2,			//遮罩层颜色
		  area: ['500px', '400px'],
		  fixed: false, //不固定
		  maxmin: true,
		  content: "${_baseUrl}/KmGroupDocumentTemplateController/selectTemplateSortTree?rand="+Math.random()
		});
}
//修改文档
function setTemplateSort(){
	var objects =  $("#defineTemplate").bootstrapTable('getSelections');
	var id ="";
	if(objects.length>1){
		layer.msg("只能修改一个模板",{time:1500});
		return;
	}else{
		for(var i =0;i<objects.length;i++){
			if(id.length>0){
				id+=","+objects[i].id;
			}else{
				id=objects[i].id;
			}
		}
		if(id.length==0){
			layer.msg("请选择文档模板");
			return;
		}
		if(judgeIfAlter(id)){
			layer.open({
				  type: 2,
				  title:'修改文档模板',
				  shade:0.2,			//遮罩层颜色
				  area: ['1000px', '280px'],
				  fixed: false, //不固定
				  maxmin: true,
				  content: "${_baseUrl}/KmGroupDocumentTemplateController/documentTemplateUpload?id="+id+"&source=define"+"&rand="+Math.random()
				});
		}else{
			layer.msg("不能修改他人模板");
			return;
		}
		
		
	}
	
}
function judgeIfAlter(id){
	var result =false;
	 var path="${_baseUrl}/KmGroupDocumentTemplateController/judgeIfAlter";
	 $.ajax({    
		type: "POST",    
		async: false,    
		url:path,   
		data:{
			"documentId":id      
		},    
		dataType:"json",    
		success: function (data) {   
			if(data.code=='1'){
				result = true;
			}else{
				result =false;
			}
		}   
	}); 
	
	return result;
}

$(document).keyup(function(event){
	  if(event.keyCode == 13){
		  defineTemplate.bootstrapTable('refresh',{url:'${_baseUrl}/KmGroupDocumentTemplateController/searchDocumentTemplate?type=define'});
	  }
});

function selectDocMessage(){
	defineTemplate.bootstrapTable('refresh',{url:'${_baseUrl}/KmGroupDocumentTemplateController/searchDocumentTemplate?type=define'});
}


/* 关闭弹出框 */
function closeWin(){
	//$("#popDiv").dialog('close');
	layer.closeAll();
}

function refreshTable(){
	defineTemplate.bootstrapTable('refresh');
}
//文档模板分类
function documentClassification(){
	var objects =  $("#defineTemplate").bootstrapTable('getSelections');
	var id = "";

	if(objects.length == 0){
		layer.msg("请选择文档模板");
		return;
	}else{
		for(var i = 0;i < objects.length;i++){
				id += objects[i].id+",";
		}
	}
	layer.open({
		  type: 2, 
		  title:'选择文档类型',
		  area: ['450px', '350px'],
		  shade: 0.3, //遮罩透明度
		  fixed: false, //不固定
		  maxmin: true,
		  shim: true,
		  content: '${_baseUrl}/KmGroupDocumentTemplateController/showTemplateSort?documentId=' + id+'&rand='+Math.random(),
		});
}

function getMenuPath(){
	var path = "${_baseUrl}/KmGroupDocumentTemplateController/getMenuPath";
	$.ajax({
		type: "POST",
		async: true,
		url:path,
		data:{
			"menuId": menuId
		},
		dataType:"json",
		success: function (data) {
			if(data.code == "1"){
				$("#menuPath").text("当前位置 ："+data.data);
			}else{
				$("#menuPath").text("当前位置 ：");
			}
		},
		error:function(data){
			layer.msg("系统错误,请联系管理员");
		}
	});
}

</script>
<style type="text/css">
.fixed-table-container{border:0px}
table{border:1px solid #ddd;margin-top: 7px!important;}
.fixed-table-header{height:0px}
</style>
</head>
<body>
<input value="${company}" type="hidden" id="company">
<div class="submenu">
	<div  class="func_header">
		<span id="menuPath"></span>
	</div>
    <div class="func_bar">
    	<div class="search_container">
	        <div class="func_search">
	        	<div class="func_search_div"></div>
	            <input type="text" id="docName" name="docName" value="${searchValue}" placeholder="请输入关键词" />
	            <a class="search_btn" onclick="selectDocMessage();" id="search" href="javascript:;"></a>
	        </div>
	    </div>
        <div class="btn_container">
           <shiro:hasPermission name="KmGroupDocumentTemplateController:selectTemplateSortTree">
			    <a class="blue_btn" href="javascript:;" onclick="defineTemplateSort();">文档模板类型定义</a>
			</shiro:hasPermission>
			<shiro:hasPermission name="KmGroupDocumentTemplateController:documentTemplateUpload">
				<a class="blue_btn" href="javascript:;" onclick="addFun();">上传文档模板</a>
			</shiro:hasPermission>
			<shiro:hasPermission name="KmGroupDocumentTemplateController:documentTemplateUpload">
				<a class="blue_btn" href="javascript:;" onclick="setTemplateSort();">修改</a>
			</shiro:hasPermission>
			<shiro:hasPermission name="KmGroupDocumentTemplateController:showTemplateSort">
                <a class="blue_btn" id="classification"  onclick="documentClassification();">分类</a>
            </shiro:hasPermission>
	        <shiro:hasPermission name="KmGroupDocumentTemplateController:useDocumentTemplate">
				<a class="blue_btn" href="javascript:;" onclick="useStatus('1');">启用</a>
			</shiro:hasPermission>
	        <shiro:hasPermission name="KmGroupDocumentTemplateController:judgeDocumentUseStatus">
				<a class="blue_btn" href="javascript:;" onclick="judgeProjectDocument()">禁用</a>
			</shiro:hasPermission>
	        <shiro:hasPermission name="KmGroupDocumentTemplateController:delDocumentTemplate">
				<a class="blue_btn" href="javascript:;" onclick="deleteFun();">删除</a>
			</shiro:hasPermission>
        </div>
    </div>
    <div style="overflow: auto; width:98%;margin: auto;">
    	<table id="defineTemplate" class="table table-bordered table_inline"></table>
	</div>
</div>
</body>
</html>